<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.2.4 or newer
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the Open Software License version 3.0
 *
 * This source file is subject to the Open Software License (OSL 3.0) that is
 * bundled with this package in the files license.txt / license.rst.  It is
 * also available through the world wide web at this URL:
 * http://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to obtain it
 * through the world wide web, please send an email to
 * licensing@ellislab.com so we can send you a copy immediately.
 *
 * @package		CodeIgniter
 * @author		EllisLab Dev Team
 * @copyright	Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
 * @license		http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

/**
 * Logging Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Logging
 * @author		EllisLab Dev Team
 * @link		http://codeigniter.com/user_guide/general/errors.html
 */
class MY_log extends CI_Log {
    //protected $_levels = array();

    /**
     * Initialize Logging class
     *
     * @return	void
     */
    public function __construct() {
        parent::__construct();
        $this->_levels = array_merge($this->_levels, array('INPUT' => 5));
    }

    // --------------------------------------------------------------------

    /**
     * Write Log File
     *
     * Generally this function will be called using the global log_message() function
     *
     * @param	string	the error level
     * @param	string	the error message
     * @param	bool	whether the error is a native PHP error
     * @return	bool
     */
    public function write_log($level = 'error', $msg, $php_error = FALSE) {
        $this->write_log_db($level, $msg, $php_error);
        parent::write_log($level, $msg, $php_error);
    }

    private function write_log_db($level = 'error', $msg, $php_error = FALSE) {
        if ($this->_enabled === FALSE) {
            return FALSE;
        }

        $level = strtoupper($level);
        if (!isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) {
            return FALSE;
        }

        if ($level === 'ERROR') {
            $this->_write_log_db($level, $msg, $php_error);
            return true;
        }

        if ($this->_explode_string('logs') == false) {
            $this->_write_log_db($level, $msg, $php_error);
            return true;
        }
    }

    private function _write_log_db($level, $msg, $php_error) {

        require APPPATH . 'config/database.php';
        $con = mysql_connect($db[$active_group]['hostname'] . ':3306', $db[$active_group]['username'], $db[$active_group]['password'], $db[$active_group]['database']);
        if (!$con) {
            die('Could not connect: ' . mysql_error($con));
        }
        mysql_select_db($db[$active_group]['database'], $con);
        mysql_query("INSERT INTO " . $db[$active_group]['database'] . ".logs (level, msg, php_error,data_hora_cadastro,user_agente,url) VALUES ('" . $level . "', '" . $msg . "','" . $php_error . "', '" . date('Y-m-d H:i:s') . "', '" . $_SERVER['HTTP_USER_AGENT'] . "', '" . str_replace('/mais_cms/', '', $_SERVER['REQUEST_URI']) . "')", $con);
        mysql_close($con);
        return TRUE;
    }

    private function _explode_string($str) {
        $ex = explode('/', $_SERVER['REQUEST_URI']);
        if (in_array($str, $ex)) {
            return true;
        } else {
            return false;
        }
    }

}

/* End of file Log.php */
/* Location: ./system/libraries/Log.php */
